// pages/detail/detail.js
var http = require("../../utils/http.js");
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product_images: [],
    product: {},
    detail: '',
    isDetail: true
  },

  switchDetail(e) {
    let value = e.target.dataset.detail;
    value = Boolean(Number.parseInt(value));

    this.setData({
      isDetail: value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id;
    // let id = 2;
    this.getProduct(Number.parseInt(id)); //加载产品的多张图片

  },
  getProduct(id) {
    let _this = this
    let cnd = ['id', '=', id];
    let domain = [cnd];
    let data = {
      model: 'wechatshop.product',
      field: 'id,name,product_image_ids,type_id,sale_price,description,product_detail',
      domain: JSON.stringify(domain)
    };
    http.request('/wechatshop/api/object/get', data).then(data => {
      if (data.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '产品不存在'
        });
      }
      let product = data[0];
      WxParse.wxParse('detail', 'html', product.product_detail, this, 5);
      product.product_images = [];
      for (let i = 0; i < product.product_image_ids.length; i++) {
        product.product_images.push({
          id: product.product_image_ids[i],
          image_url: http.requsetImageUrl('wechatshop.product.image',
            'image_url', product.product_image_ids[i])
        });
      }
      _this.setData({
        product: product,
        product_images: product.product_images
      })
    }).catch(error => {
      wx.showToast({
        icon: 'none',
        title: '连接丢失,请重新连接'
      })
    });
  },
  goCart(e) {
    wx.switchTab({
      url: '/pages/cart/cart',
      success: function (res) { },
      fail: function () { },
      complete: function () { }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})